package com.yangtao.prototype.system.log.service.impl;

import com.yangtao.framework.cache.CacheFactory;
import com.yangtao.framework.cache.CacheManager;
import com.yangtao.framework.service.BaseService;
import com.yangtao.prototype.system.dao.LogNoteDao;
import com.yangtao.prototype.system.dao.MenuTreeDao;
import com.yangtao.prototype.system.dao.UserDao;
import com.yangtao.prototype.system.domain.SysUser;
import com.yangtao.prototype.system.log.service.LogService;
import com.yangtao.prototype.system.log.web.LogModel;
import com.yangtao.wms.domain.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 杨涛
 * @date 2010-03-24
 */
@Service
public class LogServiceImpl extends BaseService implements LogService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private LogNoteDao logNoteDao;
	@Autowired
	private MenuTreeDao menuTreeDao;

	/**
	 * 用户登录
	 * 检查用户姓名，密码，过期时间
	 *
	 * @param model
	 * @return
	 */
	public LogModel login(LogModel model) {
		String userCode = model.getUserCode();
		String password = model.getPassword();
		List<SysUser> sysUsers = userDao.findByProperty("userCode", userCode);
		// 用户不存在
		if (sysUsers == null || sysUsers.size() == 0) {
			model.addError("user.not.exsit");
			return model;
		}
		SysUser currentUser = sysUsers.get(0);
		// 密码错误
		if (!currentUser.getPassword().equals(password)) {
			model.addError("password.wrong");
			return model;
		}
		// 用户过期
		Date today = new Date();
		if (currentUser.getExpireDate() != null
				&& currentUser.getExpireDate().compareTo(today) < 0) {
			model.addError("user.expired");
			return model;
		}
		model.setUser(currentUser);
		// 加载用户菜单
		model.setMenus(menuTreeDao.findAuhorisedMenu(model.getUser().getId()));
		//fix-me 将计算用户数据权限的方法设置为异步处理
		calculateUserData(currentUser.getId());
		return model;
	}

	/**
	 * 增加登录日志
	 *
	 * @param model
	 * @return
	 */
	@Override
	public LogModel addLogNote(LogModel model) {
		logNoteDao.save(model.getLn());
		return model;
	}

	/**
	 * 获取在线用户列表
	 *
	 * @param model
	 * @return
	 */
	@Override
	public LogModel getOnLineUser(LogModel model) {
		model.setPage(logNoteDao.findOnLineUser(model.getPage()));
		return model;
	}

	/**
	 * 改变用户的登录状态
	 *
	 * @param model
	 * @return
	 */
	@Override
	public LogModel changeLogNote(LogModel model) {
		logNoteDao.logOut(model.getLogId());
		return model;
	}

	/**
	 * 查找登录日志
	 *
	 * @param model
	 * @return
	 */
	@Override
	public LogModel findLogs(LogModel model) {
		logNoteDao.findPageByExample(model.getPage(), model.getLn());
		return model;
	}

	/**
	 * 模拟计算用户的主数据权限
	 * 计算用户权限
	 *
	 * @param userId
	 */
	public void calculateUserData(String userId) {
		CacheManager cache = CacheFactory.getCache();
		Map<String, List<Object>> userData = new HashMap<String, List<Object>>();
		List<Object> customers = new ArrayList<Object>();
		for (int i = 0; i < 10; i++) {
			Customer c = new Customer();
			c.setId(i + "00");
			c.setCode("cst" + i);
			c.setName("客户" + i);
			customers.add(c);
		}
		userData.put("customer", customers);
		cache.putObject(userId, userData);
	}

}
